package com.caisejingyu.whales.home.devices.printer;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Rect;
import android.os.Build;
import android.text.TextUtils;
import android.util.Log;
import android.util.Size;
import android.util.SizeF;

import java.io.File;
import java.io.FileOutputStream;

/**
 * Created by guaziorg on 11/11/16.
 * 相纸大小：874 x 1240 px
 * 相片大小：874 x 921
 * 广告大小：874 x 319
 *
 * @modify by fengxing on 16/08/167
 * 相纸大小：874 x 1240 px
 * 相片大小：874 x 992
 * 广告大小：874 x 216
 */

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class A7ImageProcessor extends AbsImageProcessor {
    private final float distance = 0.3f;

    final SizeF a7 = new SizeF(7.4f, 10.5f);
    final SizeF picNoAd = new SizeF(7.4f, 10.5f);
    final SizeF ad = new SizeF(7.4f, 1.8f);
    final SizeF picAd = new SizeF(7.4f, 8.4f);
    SizeF printer = a7;
    Paint painter;
    private PaintFlagsDrawFilter pfd;

    public A7ImageProcessor() {
        painter = new Paint(Paint.ANTI_ALIAS_FLAG);
        painter.setColor(0xFFFFFFFF);
        painter.setAntiAlias(true);
        painter.setFilterBitmap(true);
        painter.setDither(true);
        pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG
                | Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    }

    @Override
    protected String shellExec(String print) {
//        Shell.exec("/system/cups/bin/lp -o scale=100 " + print);
        String shell = format(printer.getWidth() + mOffset, printer.getHeight());
        return Shell.exec(shell + print);
    }


    //dip300
    //1 inch=2.54 cm =25.4mm

    //

    /**
     * ---------------xx
     * --           --xx
     * --   pic     --xx
     * --           --xx
     * --           --xx
     * ---------------xx
     * --   ad      --xx
     * --           --xx
     * ---------------xx
     * xxxxxxxxxxxxxxxxx
     * xxxxxxxxxxxxxxxxx
     */
    @Override
    public String process(String file, String rahmen, String advise) {
        Bitmap src = null;
        Bitmap dst = null;
        Bitmap adv = null;
        try {
            src = BitmapFactory.decodeFile(file);
            String path = RedboxEnv.INSTANCE().getPhotoCacheDir();
            File local = new File(path, "print" + ".jpg");
            Rect pixels = calCanvasSize(new Size(src.getWidth(), src.getHeight()), getPhotoSize(advise), a7);
            final float translate = convertTranslate(dpi(a7.getWidth()), pixels.width(), mOffsetPx);
            dst = Bitmap.createBitmap((int) (pixels.width() + translate), pixels.height(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(dst);
            canvas.setDrawFilter(pfd);
            canvas.drawColor(Color.WHITE);
            canvas.translate(translate, 0);
            canvas.drawRect(0, 0, pixels.width(), pixels.height(), painter);
            if (advise == null) {
                Rect clip = selectPhotoRange(new Size(src.getWidth(), src.getHeight()),
                        picNoAd);
                Rect dstTarget = calTargetSize(picNoAd.getHeight(), a7.getHeight(), pixels, true);
                canvas.drawBitmap(src, clip, dstTarget, painter);//绘制照片
                drawRahmen(rahmen, canvas, dstTarget, painter);//绘制相框
            } else {
                Rect clip = selectPhotoRange(new Size(src.getWidth(), src.getHeight()),
                        picAd);

                Rect dstTarget = calTargetSize(picAd.getHeight(), a7.getHeight(), pixels, true);
                canvas.drawBitmap(src, clip, dstTarget, painter);//绘制照片
                drawRahmen(rahmen, canvas, dstTarget, painter);//绘制相框

                adv = BitmapFactory.decodeFile(advise);

                dstTarget = calTargetSize(ad.getHeight(), a7.getHeight(), pixels, false);
                canvas.drawBitmap(adv, new Rect(0, 0, adv.getWidth(), adv.getHeight()),
                        dstTarget, painter);//绘制广告
                adv.recycle();
                adv = null;
            }

            FileOutputStream outputStream = new FileOutputStream(local);
            dst.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
            outputStream.close();
            return local.getAbsolutePath();
        } catch (Exception e) {
        } finally {
            if (src != null) {
                src.recycle();
            }
            if (dst != null) {
                dst.recycle();
            }
        }
        return null;
    }

    private SizeF getPhotoSize(String ad) {
        if (TextUtils.isEmpty(ad)) {
            return picNoAd;
        } else {
            return picAd;
        }
    }
}
